package com.lqwu.manymanythumb.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lqwu.manymanythumb.constant.ThumbConstant;
import com.lqwu.manymanythumb.model.entry.Blog;
import com.lqwu.manymanythumb.model.entry.User;
import com.lqwu.manymanythumb.model.vo.BlogVo;
import com.lqwu.manymanythumb.service.BlogService;
import com.lqwu.manymanythumb.mapper.BlogMapper;
import com.lqwu.manymanythumb.service.ThumbService;
import com.lqwu.manymanythumb.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 伍立权
* @description 针对表【blog】的数据库操作Service实现
* @createDate 2025-05-18 23:46:31
*/
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog>
    implements BlogService{

    @Resource
    private UserService userService;
    @Resource
    private ThumbService thumbService;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 获取博客详情
     * @param blogId
     * @param request
     * @return
     */
    @Override
    public BlogVo getBlogVoById(Long blogId, HttpServletRequest request) {
        // 获取博客详情
        Blog blog = this.getById(blogId);
        // 新建博客详情对象
        BlogVo blogVo = new BlogVo();
        BeanUtils.copyProperties(blog, blogVo);
        // 查询该用户是否点赞
//        LambdaQueryWrapper<Thumb> queryWrapper = new LambdaQueryWrapper<>();
        User loginUser = userService.getLoginUser(request);
//        queryWrapper.eq(Thumb::getUserId, loginUser.getId())
//                    .eq(Thumb::getBlogId, blogId);
        boolean hasThumb = thumbService.hasThumb(blogId, loginUser.getId());
//        Thumb thumb = thumbService.getOne(queryWrapper);
        blogVo.setIsThumb(hasThumb);
//        blogVo.setIsThumb(false);
//        if (thumb != null){
//            blogVo.setIsThumb(true);
//        }
        return blogVo;
    }

    /**
     * 获取博客列表
     * @param blogList
     * @param request
     * @return
     */
    @Override
    public List<BlogVo> getBlogVoList(List<Blog> blogList, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        Map<Long, Boolean> blogIdHasThumbMap = new HashMap<>();
        if (loginUser != null){
//            // 获取博客id集合
//            Set<Long> blobSet = blogList.stream().map(Blog::getId).collect(Collectors.toSet());
//
//            // 获取当前用户点赞了的博客集合
//            LambdaQueryWrapper<Thumb> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.eq(Thumb::getUserId, loginUser.getId())
//                    .in(Thumb::getBlogId, blobSet);
//            List<Thumb> thumbList = thumbService.list(queryWrapper);
//
//            // 获取当前用户点赞了的博客id,并设置到map中
//            thumbList.forEach(thumb -> {
//                blogIdHasThumbMap.put(thumb.getBlogId(), true);
//            });
            // 获取博客id集合
            List<Object> blogIdList = blogList.stream().map(blog -> blog.getId().toString()).collect(Collectors.toList());
            // 获取点赞
            List<Object> thumbList = redisTemplate.opsForHash().multiGet(ThumbConstant.USER_THUMB_KEY_PREFIX + loginUser.getId(), blogIdList);
            for (int i = 0; i < thumbList.size(); i++) {
                if (thumbList.get(i) == null) {
                    continue;
                }
                blogIdHasThumbMap.put(Long.valueOf(blogIdList.get(i).toString()), true);
            }
        }
        return blogList.stream()
                .map(blog -> {
                    BlogVo blogVo = new BlogVo();
                    BeanUtils.copyProperties(blog, blogVo);
                    blogVo.setIsThumb(blogIdHasThumbMap.get(blog.getId()));
                    return blogVo;
                })
                .toList();
    }
}